package com.nageoffer.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nageoffer.shortlink.project.dao.entity.LinkAccessStatsDo;
import com.nageoffer.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 短链接基础访问监控Mapper
 */
public interface LinkAccessStatsMapper extends BaseMapper<LinkAccessStatsDo> {

    /**
     * 记录基础访问监控数据
     * @param linkAccessStatsDo
     */
    @Insert("Insert Into t_link_access_stats (gid, full_short_url, date, pv, uv, uip, hour, weekday, create_time, update_time, del_flag) " +
            "values " +
            "(#{linkAccessStatsDo.gid},#{linkAccessStatsDo.fullShortUrl},#{linkAccessStatsDo.date},#{linkAccessStatsDo.pv},#{linkAccessStatsDo.uv},#{linkAccessStatsDo.uip}," +
            "#{linkAccessStatsDo.hour},#{linkAccessStatsDo.weekday},now(),now(),0) on DUPLICATE KEY UPDATE pv = pv +#{linkAccessStatsDo.pv}," +
            "uv = uv +#{linkAccessStatsDo.uv}," +
            "uip = uip + #{linkAccessStatsDo.uip}")
    void shortLinkStats(@Param("linkAccessStatsDo") LinkAccessStatsDo linkAccessStatsDo);



    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    date, " +
            "    SUM(pv) AS pv, " +
            "    SUM(uv) AS uv, " +
            "    SUM(uip) AS uip " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, date;")
    List<LinkAccessStatsDo> listStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内小时基础监控数据
     */
    @Select("SELECT " +
            "    hour, " +
            "    SUM(pv) AS pv " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, hour;")
    List<LinkAccessStatsDo> listHourStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据短链接获取指定日期内小时基础监控数据
     */
    @Select("SELECT " +
            "    weekday, " +
            "    SUM(pv) AS pv " +
            "FROM " +
            "    t_link_access_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, weekday;")
    List<LinkAccessStatsDo> listWeekdayStatsByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);


}
